<?php
include 'topo.php';

if(!isset($_GET["id"]))
{
    header("location:vacinacao.php");
}
$id = $_GET["id"];
$sql = "SELECT * FROM vacinacao WHERE id=$id ";
$res = pg_query($sql);
$item = pg_fetch_assoc($res);

$sql = "SELECT * FROM fazenda
 order by nome";
 $res = pg_query($sql);
 $optFazenda = "";
 while($linha=pg_fetch_assoc($res))
 {
    $id = $linha["id"];
    $nome = $linha["nome"];
    if ($item["idfazenda"]==$linha["id"]) 
    {
       $optFazenda.= "<option SELECTED value='$id'>$nome</option>";
    }
    else
    {
        $optFazenda.= "<option value='$id'>$nome</option>";
    }    
    
 }

 $sql = "SELECT * FROM animal
    order by nome";
 $res = pg_query($sql);
$optAnimal = "";
while($linha = pg_fetch_assoc($res))
{
    $id = $linha["id"];
    $nome = $linha["nome"];
    if ($item["idanimal"]==$linha["id"]) 
    {
       $optAnimal.= "<option SELECTED value='$id'>$nome</option>";
    }
    else
    {
        $optAnimal.= "<option value='$id'>$nome</option>";
    }
    
}

 $sql = "SELECT * FROM vacina
    order by nome";
 $res = pg_query($sql);
$optVacina = "";
while($linha = pg_fetch_assoc($res))
{
    $id = $linha["id"];
    $nome = $linha["nome"];
    if ($item["idvacina"]==$linha["id"]) 
    {
       $optVacina.= "<option SELECTED value='$id'>$nome</option>";
    }
    else
    {
        $optVacina.= "<option value='$id'>$nome</option>";
    }
    
}
 
?>
        
        <div style="text-align:center;font-family: arial">


            <form action="vacinacao-alterar-ok.php" method="post"> 
                <input type="hidden" name="id" value="<?php echo $item['id'];?>" />
                <table border="0" cellpadding="0" cellspacing="0" width="25%">
                    <tr> 
                        <td width="30%">Data:</td>
                        <td width="70%"><input type="Date" name="data" 
                            value="<?php echo $item["data"]; ?>"
                            required/></td>
                    </tr> 
                    <tr> 
                        <td width="30%">Fazenda:</td>
                        <td width="70%">
                        <select name="idfazenda">
                            <option value="">selecione</option>
                            <?php echo $optFazenda?>
                        </select>
                        </td>
                    </tr>
                     <tr> 
                        <td width="30%">Animal:</td>
                        <td width="70%">
                        <select name="idanimal">
                            <option value="">selecione</option>
                            <?php echo $optAnimal?>
                        </select>
                        </td>
                    </tr>
                     <tr> 
                        <td width="30%">Vacina:</td>
                        <td width="70%">
                        <select name="idvacina">
                            <option value="">selecione</option>
                            <?php echo $optVacina?>
                        </select>
                        </td>
                    </tr>
                    <tr>
                        <td width="30%">Observação:</td>
                        <td width="70%"><textarea   name="obs" 
                            required><?php echo $item["obs"]; ?></textarea></td>
                    </tr> 
                    <tr>
                        <td align="right" colspan='2'> 
                            <input type="submit" name="bot_enviar" value="Alterar"/> 
                        </td> 
                    </tr> 
                </table> 
            </form>	


        </div>
    </body>
</html>